<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Job_model extends CI_Model {

    private $_name = 'ci_user';

    var $validate = array(
        'job_edit' => array(
                    array('field'   => 'job_id'               , 'rules'   => 'is_natural|xss_clean',),
                    array('field'   => 'job_code'             , 'rules'   => 'required|is_natural|is_unique[job.job_code,job.job_id]|xss_clean',),
                    array('field'   => 'pref_id'              , 'rules'   => 'required|is_natural|xss_clean',),
                    array('field'   => 'category_id'          , 'rules'   => 'required|is_natural|xss_clean',),
                    array('field'   => 'field_id'             , 'rules'   => 'xss_clean',),
                    array('field'   => 'experience'           , 'rules'   => 'xss_clean',),
                    array('field'   => 'salary'               , 'rules'   => 'required|xss_clean',),
                    array('field'   => 'status'               , 'rules'   => 'required|is_natural|xss_clean',),
                    array('field'   => 'content_title'        , 'rules'   => 'required|xss_clean',),
                    array('field'   => 'content_industry'     , 'rules'   => 'required|xss_clean',),
                    array('field'   => 'content_category'     , 'rules'   => 'required|xss_clean',),
                    array('field'   => 'content_salary'       , 'rules'   => 'required|xss_clean',),
                    array('field'   => 'content_location'     , 'rules'   => 'required|xss_clean',),
                    array('field'   => 'content_jp_skill'     , 'rules'   => 'xss_clean',),
                    array('field'   => 'content_en_skill'     , 'rules'   => 'xss_clean',),
                    array('field'   => 'content_require'      , 'rules'   => 'xss_clean',),
                    array('field'   => 'content_contract'     , 'rules'   => 'xss_clean',),
                    array('field'   => 'content_company_size' , 'rules'   => 'xss_clean',),
                    array('field'   => 'content_status_adopt' , 'rules'   => 'xss_clean',),
                    array('field'   => 'content_others'       , 'rules'   => 'xss_clean',),
                    array('field'   => 'logo'                 , 'rules'   => 'required|xss_clean',),
                    array('field'   => 'regist_datetime'      , 'rules'   => 'required|xss_clean',),
                ),
        'job_search' => array(
                    array('field'   => 'search_category_id'   , 'rules'   => 'is_natural|xss_clean',),
                    array('field'   => 'search_pref_id'       , 'rules'   => 'is_natural|xss_clean',),
                    array('field'   => 'search_job_title'     , 'rules'   => 'xss_clean',),
                    array('field'   => 'search_job_code'      , 'rules'   => 'xss_clean',),
                    array('field'   => 'search_salary_from'   , 'rules'   => 'is_natural|xss_clean',),
                    array('field'   => 'search_salary_to'     , 'rules'   => 'is_natural|xss_clean',),
                    array('field'   => 'search_experience'    , 'rules'   => 'xss_clean',),
                ),
        );

    public function __construct(){
        parent::__construct();
        $this->load->database();
        $this->load->library(array('form_validation','MY_Validation','MY_FormParam'));
    }

    public function getJobList($params = array(), $isUsePager = false, $per_page = 0, $offset = 0, $order = 'job.regist_datetime DESC'){
        if ($isUsePager) {
            $this->db->limit($per_page, $offset);
        }

        $where = $this->getWhere($params);
        $this->db->where($where , NULL , FALSE);
        $this->db->order_by($order);
        $query = $this->db->get('job');

        if ( $query->num_rows() > 0)	{
            foreach ($query->result_array() as $rows){
                $rows['field_id'] = explode('-', $rows['field_id']);
                $result[] = $rows;
            }
            return $result;
        }
    }

    public function getJobById($job_id){
        $query = $this->db->get_where('job',array('job_id' => $job_id),1);
        if ( $query->num_rows() > 0)	{
            foreach ($query->result_array() as $rows){
                $rows['field_id'] = explode('-', $rows['field_id']);
                $result = $rows;
            }
            return $result;
        }
        return false;
    }
    
    public function getJobByCode($job_code){
        $query = $this->db->get_where('job',array('job_code' => $job_code),1);
        if ( $query->num_rows() > 0)	{
            foreach ($query->result_array() as $rows){
                $rows['field_id'] = explode('-', $rows['field_id']);
                $result = $rows;
            }
            return $result;
        }
        return false;
    }
    
    private function getWhere($params){
        $where = ' disable = 0 ';
        if(!empty($params)){
            foreach($params as $key => $value){
                if($value == ''){
                    continue;
                }
                switch ($key) {
                    case 'search_category_id':
                        if($value == 0) continue;
                        $where .= ' AND category_id = '. $value;
                        break;
                    case 'search_field_id':
                        if($value == 0) continue;
                        $where .= " AND field_id REGEXP '^$value-|-$value-|-$value$|^$value$'";
                        break;
                    case 'search_job_title':
                        $where .= ' AND content_title LIKE "%'. $value.'%"';
                        break;
                    case 'search_job_code':
                        $where .= ' AND job_code LIKE "%'. $value.'%"';
                        break;
                    case 'search_experience':
                        $where .= ' AND experience LIKE "%'. $value.'%"';
                        break;
                    case 'search_salary':
                        if($value == 0) continue;
                        $where .= ' AND salary >= '. $value;
                        break;
                    case 'search_pref_id':
                        if($value == 0) continue;
                        
                        $where .= ' AND pref_id = '. $value;
                        break;
                    case 'search_salary_from':
                    case 'search_salary_to':
                        $sql = array();
                        if($key == 'search_salary_from'){
                            $sql[] = 'salary >= '. $value;
                        }
                        if($key == 'search_salary_to'){
                            $sql[] = 'salary <= '. $value;
                        }
                        $where .= ' AND (';
                        $where .= implode(' AND ', $sql);
                        $where .= ') ';
                        break;
                    case 'search_status':
                        $where .= ' AND status = '. $value;
                        break;
                    case 'search_create_date_from':
                        $where .= ' AND regist_datetime >= "'. $value.'"';
                        break;
                    case 'search_create_date_to':
                        $where .= ' AND regist_datetime <= "'. $value.'"';
                        break;
                    default:
                        break;
                }
            }
        }
        return $where;
    }

    function exec_validation($valid_type) {
        $array    = $this->my_formparam->getFormParamList();
        $arrValidate = $this->validate[$valid_type];

        foreach($arrValidate as &$value){
            if($value['field'] == $array[$value['field']]['keyname']) {
                $value['label'] = $array[$value['field']]['disp_name'];
            }
        }

        $this->my_validation->set_rules($arrValidate);

        $status = $this->my_validation->run();

        $data['status'] = $status;
        $data['data'] = &$this->my_validation->_field_data;

        return $data;
    }
}
?>
